//      ui.c
//      
//      Copyright 2012 Mukesh <mukesh_fun2010.in>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


#include "ui.h"

GesbianUI * gesbian_ui_tab_new ( void )
{
	in ;

	GtkWidget * master = gtk_vbox_new ( FALSE, 0 ) ;
	GtkPaned * paned = gtk_hpaned_new () ;
	
	gtk_box_pack_start ( master, paned, TRUE, TRUE, 0 ) ;
	
	GtkScrolledWindow * sw1 = gtk_scrolled_window_new ( NULL, NULL ) ;
	gtk_paned_add2 ( paned, sw1 ) ;
	
	GtkListStore * store = gtk_list_store_new ( 3, 
										G_TYPE_STRING,
										G_TYPE_STRING,
										GDK_TYPE_PIXBUF ) ;
	
	GtkIconView * iconview = gtk_icon_view_new_with_model ( store ) ;
	
	gtk_icon_view_set_text_column ( iconview, 0 ) ;
	gtk_icon_view_set_pixbuf_column ( iconview, 2 ) ;
	
	gtk_icon_view_set_selection_mode ( iconview, GTK_SELECTION_MULTIPLE ) ;
	gtk_icon_view_set_orientation ( iconview, GTK_ORIENTATION_HORIZONTAL ) ;
	
	gtk_scrolled_window_set_policy ( sw1, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ) ;
	gtk_container_add ( sw1, iconview ) ;
	
	GtkVBox * preview = gtk_vbox_new ( FALSE, 5 ) ;
	GtkImage * image = gtk_image_new () ;
	
	gtk_box_pack_start ( preview, image, TRUE, TRUE, 5 ) ;
	
	GtkLabel * label = gtk_label_new ( "Preview" ) ;
	gtk_box_pack_start ( preview, label, FALSE, TRUE, 2 ) ;
	
	gtk_paned_add1 ( paned, preview ) ;
	
	/// TODO: dynamically set this
	gtk_paned_set_position ( paned, 150 ) ;
	
	GesbianUI * ui = g_slice_new ( GesbianUI ) ;
	
	ui -> paned = paned ;
	ui -> iconview = iconview ;
	ui -> iconview_sw1 = sw1 ;
	ui -> preview = preview ;
	ui -> image = image ;
	ui -> label = label ;
	ui -> store = store ;
	ui -> master = master ;
	
	/*	This is how we will check whether a ui module is initialized
	 * 	or not
	 */
	
	/// Why is this initialized here?
	
	ui -> plugin = g_slice_new ( GesbianPlugin ) ;
	ui -> plugin -> store = store ;
	
	out ;
	return ui ;
}

GesbianUI * gesbian_ui_new ( void )
{
	in ;
	
	GtkWindow * window = gtk_window_new ( GTK_WINDOW_TOPLEVEL ) ;
	GtkNotebook * notebook = gtk_notebook_new () ;
	
	g_signal_connect ( window, "destroy", gtk_main_quit, NULL ) ;
	
	GtkWidget * box = gtk_vbox_new ( FALSE, 0 ) ;
		
	gtk_box_pack_start ( box, notebook, TRUE, TRUE, 0 ) ;
	gtk_container_add ( window, box ) ;
	
	GtkToolbar * toolbar = gtk_toolbar_new () ;
	gtk_box_pack_start ( box, toolbar, FALSE, FALSE, 0 ) ;
	
	gtk_toolbar_set_style ( toolbar, GTK_TOOLBAR_BOTH_HORIZ ) ;
	
	GtkToolButton * home = gtk_tool_button_new_from_stock ( GTK_STOCK_HOME ),
					* back = gtk_tool_button_new_from_stock ( GTK_STOCK_GO_BACK ),
					* next = gtk_tool_button_new_from_stock ( GTK_STOCK_GO_FORWARD ),
					* go = gtk_tool_button_new_from_stock ( GTK_STOCK_JUMP_TO ),
					* stop = gtk_tool_button_new_from_stock ( GTK_STOCK_STOP ),
					* up = gtk_tool_button_new_from_stock ( GTK_STOCK_GO_UP ) ;
	
	GtkWidget * addressbar = gtk_combo_box_new_text () ;
	GtkEntry * locationbar = gtk_entry_new () ;
	
	gtk_combo_box_append_text ( addressbar, "Filesystem" ) ;
	
	gtk_toolbar_insert ( toolbar, back, -1 ) ;
	gtk_toolbar_insert ( toolbar, up, -1 ) ;
	gtk_toolbar_insert ( toolbar, home, -1 ) ;
	gtk_toolbar_insert ( toolbar, next, -1 ) ;
	gtk_toolbar_insert ( toolbar, stop, -1 ) ;
	
	GtkToolItem * addressbar_item = gtk_tool_item_new () ;
	gtk_container_add ( addressbar_item, addressbar ) ;
	
	gtk_toolbar_insert ( toolbar, addressbar_item, -1 ) ;
	gtk_toolbar_insert ( toolbar, go, -1 ) ;
	
	//gtk_tool_item_set_expand ( addressbar_item, TRUE ) ;
	gtk_tool_item_set_is_important ( home, TRUE ) ;
	
	GtkToolItem * locationbar_item = gtk_tool_item_new () ;
	gtk_container_add ( locationbar_item, locationbar ) ;
	
	gtk_toolbar_insert ( toolbar, locationbar_item, -1 ) ;
	
	GesbianUI * ui = gesbian_ui_tab_new () ;
	ui -> window = window ;
	ui -> notebook = notebook ;
	
	ui -> toolbar = toolbar ;
	ui -> addressbar = addressbar ;
	
	ui -> locationbar = locationbar ;
	
	ui -> title = gtk_label_new ( NULL ) ;
	//ui -> g = g ;
	
	gtk_notebook_append_page ( 	notebook, ui -> master, ui -> title ) ;
	
	GtkHBox * hbox = gtk_hbox_new ( FALSE, 0 ) ;
	gtk_box_pack_start ( box, hbox, FALSE, FALSE, 0 ) ;
	
	ui -> statusbar = gtk_statusbar_new () ;
	gtk_box_pack_start ( hbox, ui -> statusbar, TRUE, TRUE, 0 ) ;
	
	ui -> progressbar = gtk_progress_bar_new () ;
	gtk_box_pack_start ( hbox, ui -> progressbar, FALSE, FALSE, 0 ) ;
	
	gtk_statusbar_set_has_resize_grip ( ui -> statusbar, FALSE ) ;
	
	ui -> status = hbox ;
	
	ui -> plugin -> locationbar_go = gtk_tool_button_new_from_stock ( GTK_STOCK_JUMP_TO ) ;
	gtk_toolbar_insert ( toolbar, ui -> plugin -> locationbar_go, -1 ) ;
	
	ui -> spinner = gtk_spinner_new () ;
	
	/*
	GtkToolItem * item1 = gtk_tool_item_new () ;
	
	gtk_container_add ( item1, ui -> spinner ) ;
	gtk_widget_set_size_request ( ui -> spinner, 24, 24 ) ;
	
	gtk_toolbar_insert ( 	toolbar, 
							item1,
							-1 ) ;
	
	gtk_tool_item_set_is_important ( item1, 1 ) ;
	*/
	
	gtk_box_pack_start ( hbox, ui -> spinner, FALSE, FALSE, 5 ) ;
	g_signal_connect ( go, "clicked", gesbian_ui_go_button, ui ) ;
	
	ui -> plugin -> window = window ;
	
	ui -> plugin -> spinner = ui -> spinner ;
	ui -> plugin -> title = ui -> title ;
	ui -> plugin -> progressbar = ui -> progressbar ;

	ui -> plugin -> statusbar = ui -> statusbar ;
	ui -> plugin -> addressbar = ui -> addressbar ;
	ui -> plugin -> addressbar_button = go ;
	ui -> plugin -> iconview = ui -> iconview ;
	
	ui -> plugin -> locationbar = locationbar ;
	ui -> plugin -> back = back ;
	ui -> plugin -> next = next ;
	ui -> plugin -> up = up ;
	ui -> plugin -> home = home ;
	ui -> plugin -> stop = stop ;
	
	ui -> plugin -> preview_label = ui -> label ;
	ui -> plugin -> preview_image = ui -> image ;
	
	gtk_tool_item_set_expand ( locationbar_item, TRUE ) ;
	gtk_label_set_line_wrap ( ui -> plugin -> preview_label, 1 ) ;

	gtk_widget_set_no_show_all ( ui -> spinner, 1 ) ;
	gtk_widget_set_no_show_all ( ui -> progressbar, 1 ) ;
	
	gtk_widget_set_sensitive ( locationbar, 0 ) ;
	gtk_widget_set_sensitive ( ui -> plugin -> locationbar_go, 0 ) ;
	gtk_widget_set_sensitive ( stop, 0 ) ;
		
	gtk_window_resize ( window, 530, 300 ) ;
	gtk_widget_show_all ( window ) ;
	
	gtk_widget_hide ( ui -> progressbar ) ;
	
	out ;
	return ui ;
}

void gesbian_ui_update ( void )
{
	while ( gtk_events_pending ())
		gtk_main_iteration_do ( FALSE ) ;
}

void gesbian_ui_go_button ( GtkWidget * widget, GesbianUI * ui )
{
	gesbian_ui_load_module ( ui, gtk_combo_box_get_active ( ui -> addressbar )) ;
}

void gesbian_ui_load_module ( GesbianUI * ui, GesbianModules modules )
{
	GesbianModule * module = gesbian_module_load ( ui -> plugin, modules ) ;
	ui -> module = module ;
	
}
